package com.bl.weilan.service.biz.trade.impl;

import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.*;
import com.bl.weilan.dao.repository.*;
import com.bl.weilan.service.biz.company.CompanyService;
import com.bl.weilan.service.biz.companyfinancing.CompanyFinancingService;
import com.bl.weilan.service.biz.institution.InstitutionService;
import com.bl.weilan.service.biz.trade.TradeService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.tradeb.*;
import com.bl.weilan.service.vo.tradec.TradeCAddVo;
import com.bl.weilan.service.vo.tradec.TradeCListVo;
import com.bl.weilan.service.vo.tradec.TradeVo;
import com.bl.weilan.service.vo.traded.TradeDAddVo;
import com.bl.weilan.service.vo.traded.TradeDListVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class TradeServiceImpl implements TradeService {

    @Autowired
    TradeARepository tradeARepository;

    @Autowired
    TradeCRepository tradeCRepository;

    @Autowired
    TradeBRepository tradeBRepository;

    @Autowired
    CompanyService companyService;

    @Autowired
    CompanyFinancingService companyFinancingService;

    @Autowired
    InstitutionCompanyRepository institutionCompanyRepository;

    @Autowired
    TradeInstitutionRepository tradeInstitutionRepository;

    @Autowired
    CompanyOpponentRepository companyOpponentRepository;

    @Autowired
    TradeDRepository tradeDRepository;

    @Autowired
    InstitutionService institutionService;

    @Autowired
    TradePolicyRepository tradePolicyRepository;

    @Autowired
    TradeReportRepository tradeReportRepository;

    @Autowired
    TradeChartRepository tradeChartRepository;

    @Autowired
    CompanyTradeRepository companyTradeRepository;

    @Override
    public TradeCListVo getTradeCList(String tradebId, Pageable pageable) {
        Page<TradeCEntity> tradeCEntities;
        TradeCListVo tradeCListVo = new TradeCListVo();
        tradeCEntities = tradeCRepository.findByTradeBOrderByWeightDesc(tradebId,pageable);
        tradeCListVo.setTotalCount(tradeCEntities.getTotalElements());
        tradeCListVo.setList(tradeCEntities.getContent());
        return tradeCListVo;
    }

    @Override
    public TradeBListVo getTradeBList(String tradeaId, Pageable pageable) {
        Page<TradeBEntity> tradeBEntities;
        TradeBListVo tradeBListVo = new TradeBListVo();
        tradeBEntities = tradeBRepository.findByTradeAOrderByWeightDesc(tradeaId,pageable);
        tradeBListVo.setTotalCount(tradeBEntities.getTotalElements());
        tradeBListVo.setList(tradeBEntities.getContent());
        return tradeBListVo;
    }

    @Override
    public TradeBCListVo getTradeBCList(String tradeaId) {

        TradeBCListVo tradeBCListVo = new TradeBCListVo();
        List<TradeBCVo> tradeBCVoList = new ArrayList<>();

        List<TradeBEntity> tradeBEntities;
        TradeBListVo tradeBListVo = new TradeBListVo();
        tradeBEntities = tradeBRepository.findByTradeAOrderByWeightDesc(tradeaId);
        for (TradeBEntity tradeBEntity : tradeBEntities) {
            TradeBCVo tradeBCVo = new TradeBCVo();
            tradeBCVo.setIcon1(tradeBEntity.getIcon1());
            tradeBCVo.setIcon2(tradeBEntity.getIcon2());
            tradeBCVo.setTradebId(tradeBEntity.getId());
            tradeBCVo.setTradebName(tradeBEntity.getName());

            List<TradeCVo> tradeCVos = new ArrayList<>();
            List<TradeCEntity> tradeCEntities;
            tradeCEntities = tradeCRepository.findByTradeBOrderByWeightDesc(tradeBEntity.getId());
            for (TradeCEntity tradeCEntity : tradeCEntities) {
                TradeCVo tradeCVo = new TradeCVo();
                tradeCVo.setTradecId(tradeCEntity.getId());
                tradeCVo.setTradecName(tradeCEntity.getName());
                tradeCVos.add(tradeCVo);
            }
            tradeBCVo.setTradeCVoList(tradeCVos);
            tradeBCVoList.add(tradeBCVo);
        }

        tradeBCListVo.setTotalCount(tradeBEntities.size());
        tradeBCListVo.setList(tradeBCVoList);
        return tradeBCListVo;
    }

    @Override
    public TradeAEntity getTradeADetail(String id) {
        return null;
    }

    @Override
    public TradeBEntity getTradeBDetail(String id) {
        return null;
    }

    @Override
    public TradeVo getTradeDetail(String id) {
        String tradeType = "";

        TradeCEntity tradeCEntity = new TradeCEntity();
        TradeBEntity tradeBEntity = new TradeBEntity();
        tradeCEntity = tradeCRepository.findOne(id);
        TradeVo tradeVo = new TradeVo();
//        id为trade_c的id
        if(tradeCEntity != null) {
            tradeType = "TRADEC";
            BeanUtils.copyProperties(tradeCEntity, tradeVo);
            tradeVo.setChildNum(0);
            tradeVo.setLevel("三级");
            tradeVo.setIcon(tradeCEntity.getIcon());
            tradeVo.setTradeType("TRADEC");
        } else {
//        id为trade_b的id
            tradeBEntity = tradeBRepository.findOne(id);
            tradeType = "TRADEB";
            if(tradeBEntity != null) {
                BeanUtils.copyProperties(tradeBEntity, tradeVo);
                List<TradeCEntity> tradeCEntities = tradeCRepository.findByTradeBOrderByWeightDesc(id);
                // 子行业
                tradeVo.setLevel("二级");
                tradeVo.setIcon1(tradeVo.getIcon1());
                tradeVo.setIcon2(tradeVo.getIcon2());
                tradeVo.setTradeType("TRADEB");
                tradeVo.setChildNum(tradeCEntities.size());
            }
        }

        // 添加公司数目
        tradeVo.setCompanyNum(getCompanyCountByTradeId(id, tradeType));
        // 行业今年融资金额
        tradeVo.setAnnualFinanc(getFinancingLisByTradeIdThisYear(id, tradeType));
        // 行业估值资金额
        tradeVo.setValuations(getAluationsLisByTradeId(id, tradeType));

        // 行业机构列表
//        tradeVo.setInstitutionListVo(institutionService.getInstitutionLists(id));

        // 机构数量
        tradeVo.setInstitutionNum(institutionService.getInstitutionNum(id));

        // 公司列表
//        tradeVo.setCompanyListVo(companyService.getCompanyListsByTrade(id, tradeType));

        return tradeVo;
    }

    @Override
    public int getCompanyCountByTradeId(String tradeId, String tradeType) {
        return companyService.getCompanyCounByTrade(tradeId, tradeType);
    }

    @Override
    public float getFinancingLisByTradeIdThisYear(String tradeId, String tradeType) {
        float total = companyService.getFinicingByTrade(tradeId, tradeType);
        total = (float)(Math.round(total*100))/100;
        return total;
    }

    @Override
    public float getAluationsLisByTradeId(String tradeId, String tradeType) {
        float valuations = companyService.getAluationsLisByTradeId(tradeId, tradeType);
        valuations = (float)(Math.round(valuations*100))/100;
        return valuations;
    }

    @Override
    public void addTradeD(TradeDAddVo tradeDAddVo) {
        TradeDEntity tradeDEntity = new TradeDEntity();
        tradeDEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(tradeDAddVo, tradeDEntity);
        tradeDRepository.save(tradeDEntity);
    }

    @Transactional
    @Override
    public void del(DelBody id) {
        companyOpponentRepository.deleteByTradeDId(id.getId());
        tradeDRepository.delete(id.getId());
    }

    @Override
    public void addCompanyTradeD(String companyId, String tradedId){
//        CompanyOpponentEntity companyOpponentEntity = new CompanyOpponentEntity();
        List<CompanyOpponentEntity> companyOpponentEntities = new ArrayList<>();
        companyOpponentEntities = companyOpponentRepository.findByCompanyId(companyId);
        TradeDEntity  tradeDEntity = new TradeDEntity();
        tradeDEntity = tradeDRepository.findOne(tradedId);
        String tradedName = tradeDEntity.getName();
        for(CompanyOpponentEntity companyOpponentEntity:companyOpponentEntities){
            companyOpponentEntity.setTradeDName(tradedName);
            companyOpponentEntity.setTradeDId(tradedId);
            companyOpponentEntity.setUpdateTime(new Date());
            companyOpponentRepository.save(companyOpponentEntity);
        }
    }

    @Override
    public TradeDListVo getTradeDList(String tradecId){
        TradeDListVo tradeDListVo = new TradeDListVo();
        List<TradeDEntity> tradeDEntities = new ArrayList<>();
        tradeDEntities = tradeDRepository.findByTradeC(tradecId);
        tradeDListVo.setTotalCount(tradeDEntities.size());
        tradeDListVo.setList(tradeDEntities);
        return tradeDListVo;
    }

    @Override
    public TradeDListVo getTradeDListByTradeCAndCompanyId(String companyId,String tradeCId) {
        TradeDListVo tradeDListVo = new TradeDListVo();
        List<TradeDEntity> tradeDEntities = new ArrayList<>();
        List<TradeDEntity> companyTradeDList = new ArrayList<>();
        tradeDEntities = tradeDRepository.findByTradeC(tradeCId);
        for (TradeDEntity tradeDEntity : tradeDEntities) {
            List<CompanyOpponentEntity> companyOpponentEntityList = companyOpponentRepository.findByCompanyIdAndTradeDId(companyId, tradeDEntity.getId());
            if (companyOpponentEntityList != null && companyOpponentEntityList.size() > 0) {
                companyTradeDList.add(tradeDEntity);
            }
        }

        tradeDListVo.setTotalCount(companyTradeDList.size());
        tradeDListVo.setList(companyTradeDList);
        return tradeDListVo;
    }


//    @Override
//    public InstitutionListVo getInstitutionLisByTradeId(String tradeId, String tradeType) {
//
//        return null;
//    }

    //增加三级行业
    @Override
    public void addTradeC(TradeCAddVo tradeCAddVo) {
        TradeCEntity tradeCEntity = new TradeCEntity();
        tradeCEntity.setId(UUIDUtils.getUUID());
        tradeCEntity.setCreateTime(new Date());
        tradeCEntity.setUpdateTime(new Date());
        BeanUtils.copyProperties(tradeCAddVo, tradeCEntity);
        tradeCRepository.save(tradeCEntity);

        addTradeChart(tradeCEntity.getId(),"bar","投资事件","件");
        addTradeChart(tradeCEntity.getId(),"polyline","投资金额","亿");
        addTradeChart(tradeCEntity.getId(),"polyline","总估值","亿");
        addTradeChart(tradeCEntity.getId(),"bars","投资轮次","件");
        addTradeChart(tradeCEntity.getId(),"pie","成立时间","家");
        addTradeChart(tradeCEntity.getId(),"pie","成立地点","家");
    }

    //增加二级行业
    @Override
    public void addTradeB(TradeBAddVo tradeBAddVo) {
        TradeBEntity tradeBEntity = new TradeBEntity();
        tradeBEntity.setId(UUIDUtils.getUUID());
        tradeBEntity.setCreateTime(new Date());
        tradeBEntity.setUpdateTime(new Date());
        BeanUtils.copyProperties(tradeBAddVo, tradeBEntity);
        tradeBRepository.save(tradeBEntity);
        addTradeChart(tradeBEntity.getId(),"bar","投资事件","件");
        addTradeChart(tradeBEntity.getId(),"polyline","投资金额","亿");
        addTradeChart(tradeBEntity.getId(),"polyline","总估值","亿");
        addTradeChart(tradeBEntity.getId(),"bars","投资轮次","件");
        addTradeChart(tradeBEntity.getId(),"pie","成立时间","家");
        addTradeChart(tradeBEntity.getId(),"pie","成立地点","家");
    }

    public void addTradeChart(String tradeId, String type, String name, String unit) {
        TradeChartEntity tradeChartEntity = new TradeChartEntity();
        tradeChartEntity.setId(UUIDUtils.getUUID());
        tradeChartEntity.setCreateTime(new Date());
        tradeChartEntity.setUpdateTime(new Date());
        tradeChartEntity.setTradeId(tradeId);
        tradeChartEntity.setType(type);
        tradeChartEntity.setUnit(unit);
        tradeChartEntity.setName(name);
        tradeChartRepository.save(tradeChartEntity);
    }

    @Transactional
    @Override
    public void delTradeC(DelBody id) {
        String tradeId = id.getId();
        tradeCRepository.delete(id.getId());
//      chart表，chartdetail表，trade_d,institution,policy,report,company_trade,company_opponent
//      trade_chart表，trade_char_detail表没有删
//      删除三级行业关联的四级行业+竞争对手
        List<TradeDEntity> tradeDEntities = new ArrayList<>();
        tradeDEntities = tradeDRepository.findByTradeC(tradeId);
        if(tradeDEntities != null) {
            for (TradeDEntity tradeDEntity : tradeDEntities) {
                String tradedId = tradeDEntity.getId();
                companyOpponentRepository.deleteByTradeDId(tradedId);
                tradeDRepository.delete(tradedId);
            }
        }

//        删除三级行业关联的机构信息
        List<TradeInstitutionEntity> tradeInstitutionEntities = new ArrayList<>();
        tradeInstitutionEntities = tradeInstitutionRepository.findByTradeId(tradeId);
        if(tradeInstitutionEntities != null){
            for (TradeInstitutionEntity tradeInstitutionEntity : tradeInstitutionEntities) {
                tradeInstitutionRepository.delete(tradeInstitutionEntity.getId());
            }
        }

//        删除三级行业关联政策信息
        List<TradePolicyEntity> tradePolicyEntities = new ArrayList<>();
        tradePolicyEntities = tradePolicyRepository.findByTradeId(tradeId);
        if(tradePolicyEntities != null){
            for (TradePolicyEntity tradePolicyEntity : tradePolicyEntities) {
                tradePolicyRepository.delete(tradePolicyEntity.getId());
            }
        }

//       删除三级行业相关报道信息
        List<TradeReportEntity> tradeReportEntities = new ArrayList<>();
        tradeReportEntities = tradeReportRepository.findByTradeId(tradeId);
        if(tradeReportEntities != null) {
            for (TradeReportEntity tradeReportEntity : tradeReportEntities) {
                tradeReportRepository.delete(tradeReportEntity.getId());
            }
        }

//        删除三级行业关联公司信息
        List<CompanyTradeEntity> companyTradeEntities = new ArrayList<>();
        companyTradeEntities = companyTradeRepository.findByTradeC(tradeId);
        if(companyTradeEntities != null) {
            for (CompanyTradeEntity companyTradeEntity : companyTradeEntities) {
                companyTradeRepository.delete(companyTradeEntity.getId());
            }
        }
    }


    @Transactional
    @Override
    public void delTradeB(DelBody id) {
        String tradeId = id.getId();
        tradeBRepository.delete(id.getId());

//      删除二级行业关联的四级行业+竞争对手
        List<TradeDEntity> tradeDEntities = new ArrayList<>();
        tradeDEntities = tradeDRepository.findByTradeB(tradeId);
        if(tradeDEntities != null) {
            for (TradeDEntity tradeDEntity : tradeDEntities) {
                String tradedId = tradeDEntity.getId();
                companyOpponentRepository.deleteByTradeDId(tradedId);
                tradeDRepository.delete(tradedId);
            }
        }

//      删除二级行业关联的三级行业+竞争对手
        List<TradeCEntity> tradeCEntities = new ArrayList<>();
        tradeCEntities = tradeCRepository.findByTradeBOrderByWeightDesc(tradeId);
        if(tradeCEntities != null) {
            for (TradeCEntity tradeCEntity : tradeCEntities) {
                String tradecId = tradeCEntity.getId();
                DelBody delBody = new DelBody();
                delBody.setId(tradecId);
                this.delTradeC(delBody);
            }
        }

//        删除二级行业关联的机构信息
        List<TradeInstitutionEntity> tradeInstitutionEntities = new ArrayList<>();
        tradeInstitutionEntities = tradeInstitutionRepository.findByTradeId(tradeId);
        if(tradeInstitutionEntities != null){
            for (TradeInstitutionEntity tradeInstitutionEntity : tradeInstitutionEntities) {
                tradeInstitutionRepository.delete(tradeInstitutionEntity.getId());
            }
        }

//        删除二级行业关联政策信息
        List<TradePolicyEntity> tradePolicyEntities = new ArrayList<>();
        tradePolicyEntities = tradePolicyRepository.findByTradeId(tradeId);
        if(tradePolicyEntities != null){
            for (TradePolicyEntity tradePolicyEntity : tradePolicyEntities) {
                tradePolicyRepository.delete(tradePolicyEntity.getId());
            }
        }

//       删除二级行业相关报道信息
        List<TradeReportEntity> tradeReportEntities = new ArrayList<>();
        tradeReportEntities = tradeReportRepository.findByTradeId(tradeId);
        if(tradeReportEntities != null) {
            for (TradeReportEntity tradeReportEntity : tradeReportEntities) {
                tradeReportRepository.delete(tradeReportEntity.getId());
            }
        }

//        删除二级行业关联公司信息
        List<CompanyTradeEntity> companyTradeEntities = new ArrayList<>();
        companyTradeEntities = companyTradeRepository.findByTradeB(tradeId);
        if(companyTradeEntities != null) {
            for (CompanyTradeEntity companyTradeEntity : companyTradeEntities) {
                companyTradeRepository.delete(companyTradeEntity.getId());
            }
        }
    }

    @Override
    public String getTradeBId(String fullName) {
        TradeBEntity tradeBEntity= tradeBRepository.findByName(fullName);
        return tradeBEntity.getId();
    }

    @Override
    public String getTradeCId(String fullName) {
        TradeCEntity tradeCEntity= tradeCRepository.findByName(fullName);
        return tradeCEntity.getId();
    }

}